package cn.org.xiaosheng.hutool;

import cn.org.xiaosheng.hutool.utils.PostUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author XiaoSheng
 * @date 2023-11-17
 * @dec 测试Post接口远程调用
 */
@RestController
public class PostTest {

    /**
     * 测试post请求, 入参方式为form-data或者application/x-www-form-urlencoded方式
     * @return
     */
    @RequestMapping("/postTest")
    public String postTest() {
        HashMap<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("appKey", "bee95af8xxx6ddc0f1e");
        bodyMap.put("appSecret", "ddc779b64xxx0");
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        String post = PostUtils.postFormData("https://xxx/api/lapp/token/get", bodyMap);
        System.out.println(post);
        return post;
    }

    /**
     * 测试JSON请求方式 多级嵌套方式
     * 具体报文:
     * {
     *     "agentId": "eba5652793ee462f8cf2e78109e49326",
     *     "deviceid": "3C6A2CFFFED345C4",
     *     "accessToken": "2AHGxxJkKp",
     *     "topic": "api/device/3C6A2CFFFED345C4/cloud",
     *     "dtype": 1,
     *     "sdata": "{\"type\":\"zigbeeConfig\",\"command\":\"epList\",\"session\":\"\",\" \":1024}",
     *     "ts": "586851710"
     * }
     * @return
     */
    @RequestMapping("/postTest1")
    public String postTest1() {
        HashMap<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("agentId", "eba5652793ee462f8cf2e78109e49326");
        bodyMap.put("deviceid", "3C6A2CFFFED345C4");
        bodyMap.put("accessToken", "2AHGWxxkKp");
        bodyMap.put("topic", "api/device/3C6A2CFFFED345C4/cloud");
        bodyMap.put("dtype", "1");
        bodyMap.put("ts", "586851710");
        Map<String, Object> sData = new HashMap<String, Object>();
        sData.put("type","zigbeeConfig");
        sData.put("command", "epList");
        sData.put("session", "");
        sData.put("sequence", "1024");
        bodyMap.put("sdata", JSONObject.toJSONString(sData));
        // 可以不需要传递请求头, 会根据你传入的川参加进行判断,从而确定是哪种请求头
        String s = PostUtils.postJSONData("http://xxxxx/owon-data/5000/agent/sendMqttData", JSONObject.toJSONString(bodyMap));
        return s;
    }




}
